<?php
namespace app\wxpro\controller;
use think\Db;
use think\Controller;
use think\Log;
class Applet extends Controller{
	public function index(){
		echo "1";
	}
	public function getopenid(){
		
	}
    public function openid(){
		$code = request()->get('code');
        $appid = "wx6e64e3c7372be385";//小程序的唯一标识
        $secret = "739ec2a4762e9e9a983107f2e513aff7";//小程序的AppSecret
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret=' . $secret . '&js_code=' . $code . '&grant_type=authorization_code';
		$curl = curl_init();//启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);//要访问的地址
        curl_setopt($curl, CURLOPT_HEADER, 1);//设置header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);//设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//跳过ssl检查项
        curl_setopt($curl, CURLOPT_HEADER, 0);//显示返回的Header区域内容
        $tmpInfo = curl_exec($curl);//执行操作
        // $err = curl_error($curl);//输出错误
        curl_close($curl);//关闭CURL会话
        // $tmpInfo = json_decode(json_encode(simplexml_load_string($tmpInfo, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        $tmpInfo = $this->object_to_array(json_decode($tmpInfo));
		Log::write($tmpInfo,"用户信息");
        if (!empty($tmpInfo['errmsg'])) {
			return json(['status' => '0',"error"=>$tmpInfo['errmsg']]);
            return $tmpInfo['errmsg'];
        } else {
			return json(['status' => '1',"openid"=>$tmpInfo['openid']]);
            $user['user_id'] = $tmpInfo['openid'];
            return json_encode($user['user_id']);
        }
    }
	public function formIdAdd(){
		$wxpro_form["ctime"]=time();
		$wxpro_form["openid"]=request()->get("openid");
		$wxpro_form["form_id"]=request()->get("formId");
		$wxpro_form["num"]=1;
		$res=Db::table("wxpro_form")->insert($wxpro_form);
	}
	
    public function object_to_array($obj){
        $_arr = is_object($obj) ? get_object_vars($obj) :$obj;
        foreach($_arr as $key=>$val){
            if($key !== 'gioouts'){
                $val = (is_array($val) || is_object($val)) ? $this->object_to_array($val):$val;
                $arr[$key] = $val;
            }
        }
        return $arr;
    }
    
    
}